package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.pojo.Talents;
import org.example.pojo.TalentsRegister;
import org.example.service.TalentsService;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Controller
@Api(tags = "个人简历管理")
@RequestMapping("/talents")
public class BioController {


    @Autowired
    TalentsService talentsService;

    @ResponseBody
    @PostMapping ("/talentsRegister")
    @ApiOperation("注册数据")
    public ResponseData register(@ApiParam("注册数据有：名字，性别，学历，身高，手机号码，登录密码，确认密码") TalentsRegister talentsRegister){

        boolean b = talentsService.registerTalent(talentsRegister);

        if (b){
            return ResponseData.getSccesss("注册成功",100);
        }else {
            return ResponseData.error("失败",101);
        }

    }

    @ResponseBody
    @GetMapping("/isPhoneRegister/{phone}")
    @ApiOperation("查看注册是否成功")
    public ResponseData isPhoneRegister(@ApiParam("有手机号码")  @PathVariable String phone){

        TalentsRegister existPhone = talentsService.isExistPhone(phone);


        return existPhone==null?ResponseData.getSccesss("可以注册 ",101):ResponseData.error("该用户以存在", 100);
    }

    @ResponseBody
    @PostMapping("/talentsLogin/{phone}/{passwrod}")
    @ApiOperation("登录数据 ")
    public ResponseData Login(@ApiParam("有手机号码")  @PathVariable String phone, @ApiParam("登录密码") @PathVariable String passwrod){

        HashMap<String, String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("passwrod",passwrod);


        Talents talents = talentsService.passwordAndphoneLoginTalent(map);

        return talents==null?ResponseData.getSccesss("登录失败 ",101):ResponseData.allResponse(talents, "登录成功",100,0);
    }


    @ResponseBody
    @GetMapping("/showLoginAndPasswrod/{id}")
    @ApiOperation("人才查询所有的数据")
    public ResponseData showLoginAndPasswrod(@ApiParam("登录后账号id")@PathVariable Integer id){
        Talents talents = talentsService.showLoginAndPasswrod(id);


        return ResponseData.getJsonData(talents,0);
    }






    @ResponseBody
    @PutMapping("/updateTalentsRegister/{id}/{passwrod}/{newPassword}")
    @ApiOperation("修改密码")
    public ResponseData updateTalentsRegister(@ApiParam("用户名")  @PathVariable Integer id,@ApiParam("登录密码") @PathVariable String passwrod,@ApiParam("修改的密码") @PathVariable String newPassword){

        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("passwrod",newPassword);

        String tPassword = talentsService.selectPassWrod(id);

        if (passwrod.equals(tPassword)){
            boolean b = talentsService.updateTalentsRegister(map);

            return b?ResponseData.getSccesss("修改成功",100):ResponseData.error("修改失败", 101);

        }else {

            return ResponseData.error("密码错误", 102);
        }
    }


    @ResponseBody
    @PostMapping("/addUpdateTalents")
    @ApiOperation("根据修改人才资料")
    public ResponseData updateTalentsRegister(@ApiParam("人才数据")@RequestBody Talents talents){

        boolean b = talentsService.addUpdateTalents(talents);
        return b?ResponseData.getSccesss("修改成功",100):ResponseData.error("修改失败", 101);
    }

}
